package vn.aptech.leaveManagementSystem.view.admin;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JButton;

import com.toedter.calendar.JDateChooser;

import vn.aptech.leaveManagementSystem.business.dto.Department;
import vn.aptech.leaveManagementSystem.business.dto.Holiday;
import vn.aptech.leaveManagementSystem.business.model.MyModel;
import vn.aptech.leaveManagementSystem.business.parse.MyParse;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DemoUpdateDepartment extends JFrame{
	private JButton btnUpdate;
	private JButton btnCancel;
	private MyModel mymodel=new MyModel();
	AdminForm mainform;
	int idDepParameter;
	private JTextField txtDepName;

	public DemoUpdateDepartment(AdminForm mainform,int idDep) {
		this.mainform=mainform;
		idDepParameter=idDep;
		initGUI();
		initData();
		initAction();
	}

	private void initAction() {
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					ResultSet rs=mymodel.departments.getDepartment(idDepParameter);
					if (rs.next())
					{
						Department tempDep=MyParse.toDepartment(rs);
						tempDep.setDepartmentName(txtDepName.getText());
						mymodel.departments.updateDepartment(tempDep);
						close();
						JOptionPane.showMessageDialog(null, "Update successful!");
					}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
			}
		});
	}

	private void initData() {
		UpdateData();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initGUI() {
		setBounds(100, 100, 450, 135);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);
		setTitle("Update Holiday");
		getContentPane().setLayout(null);
		setCloseForm();
		
		JLabel lblDepName = new JLabel("Department Name:");
		lblDepName.setBounds(10, 19, 112, 14);
		getContentPane().add(lblDepName);
		
		btnUpdate = new JButton("Update");
		btnUpdate.setIcon(new ImageIcon("icons/gif/16x16/Update.gif"));
		btnUpdate.setBounds(132, 60, 89, 23);
		getContentPane().add(btnUpdate);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setIcon(new ImageIcon("icons/gif/16x16/Cancel.gif"));
		btnCancel.setBounds(231, 60, 103, 23);
		getContentPane().add(btnCancel);		
		
		txtDepName = new JTextField();
		txtDepName.setBounds(116, 16, 316, 20);
		getContentPane().add(txtDepName);
		txtDepName.setColumns(10);
	}
	
	public void setCloseForm()
	{
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				JFrame jframe=(JFrame) e.getSource();
				mainform.setEnabled(true);
				setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			}
		});
	}
	
	public void UpdateData()
	{
		try {
			ResultSet rs=mymodel.departments.getDepartment(idDepParameter);
			if (rs.next())
			{
				Department tempDep=MyParse.toDepartment(rs);
				txtDepName.setText(tempDep.getDepartmentName());
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void close()
	{
		toBack();
		dispose();
		mainform.setEnabled(true);
		mainform.updateDataTableDepartment();
	}
	
	
}
